package com.edu.hre.common.crontab;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import org.apache.commons.lang.StringUtils;
import org.hibernate.mapping.Array;
import com.edu.hre.common.email.EMailObject;
import com.edu.hre.common.email.EMailSender;
import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.ServerConfig;
import com.edu.hre.db.pojo.HreEmployee;
import com.edu.hre.db.pojo.HreSubunit;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;

public class NotifyEMailBirthday {
	
	private ServerConfig serverConfig = null;
	private BaseService baseService = null;
	private EMailSender eMailSender = null;
	public static final String BEAN_NAME = "notifyEMailBirthday";
	private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM");
	private static final SimpleDateFormat dateFormat2 = new SimpleDateFormat("dd/MM/yyyy");
	
	public synchronized void run(){
		int offset = 0;
		int limit = 1000;
		List<EMailObject> listEmails = new ArrayList<EMailObject>();
		// System.out.println(serverConfig.getBirthdayMessage());
		while(true){
			List<HreEmployee> listEmp = baseService.getPojos(null, -1, null, null, null, limit, offset, HreEmployee.class);
			if(listEmp == null || listEmp.size() == 0){
				break;
			}
			offset += limit;
			for(int i = 0; listEmp != null && i < listEmp.size(); i++){
				try {
					HreEmployee emp = listEmp.get(i);
					Integer tempInt = interval2Days(new Date(), emp.getEmpBirthday());
					if(tempInt == null){
						continue;
					}
					Calendar cal = Calendar.getInstance();
					cal.setTime(emp.getEmpBirthday());
					cal.add(Calendar.YEAR, -1900);
					if(tempInt != 0){
						 EMailObject email = new EMailObject();
						 email.setAddress(serverConfig.getDefaultAddressEmail()); 
						 email.setSubject("Sắp đến sinh nhật: " + CommonUtils.getFullName(emp));
						 String content = "Dear mọi nguời!\nCòn " + tempInt + " ngày nữa là đến sinh nhật bạn "
								 + CommonUtils.getEmpNameAndUsername(emp) + " - " + dateFormat2.format(cal.getTime());
						 if(emp.getWorkStation() != null){
							 HreSubunit subunit = baseService.getFirstPojo("id",Compare.EQUAL, emp.getWorkStation(), HreSubunit.class);
							 if(subunit != null){
								 content += ", nhân sự  thuộc phòng ban: " + subunit.getName();
							 }
						 }
						 content += "\n\n" + serverConfig.getDefaultEmailSignature();
						 email.setContent(content);
						 email.setHTMLStyle(false);
						 listEmails.add(email);
					} else {
						 EMailObject email = new EMailObject();
						 email.setAddress(serverConfig.getDefaultAddressEmail()); 
						 email.setSubject("Sinh nhật: " + CommonUtils.getFullName(emp));
						 String content = "Dear mọi nguời!\nHôm nay là sinh nhật bạn "
								 + CommonUtils.getEmpNameAndUsername(emp) + " - " + dateFormat2.format(cal.getTime());
						 if(emp.getWorkStation() != null){
							 HreSubunit subunit = baseService.getFirstPojo("id",Compare.EQUAL, emp.getWorkStation(), HreSubunit.class);
							 if(subunit != null){
								 content += ", nhân sự  thuộc phòng ban: " + subunit.getName();
							 }
						 }
						 content += "\n\n" + serverConfig.getDefaultEmailSignature();
						 email.setContent(content);
						 email.setHTMLStyle(false);
						 listEmails.add(email);  
						 
						 //-================ send to emp ============================
						 if(StringUtils.isNotBlank(emp.getEmpWorkEmail())){
							 email = new EMailObject();
							 email.setAddress(emp.getEmpWorkEmail()); 
							 email.setSubject("Happy birthday to you!");
							 content = serverConfig.getBirthdayMessage().replace("@name", emp.getEmpFirstname().toUpperCase());
							 content += "\n\n" + serverConfig.getDefaultEmailSignature();
							 email.setContent(content);
							 email.setHTMLStyle(false);
							 listEmails.add(email);
						 }
						 if(StringUtils.isNotBlank(emp.getEmpOthEmail())){
							 email = new EMailObject();
							 email.setAddress(emp.getEmpOthEmail()); 
							 email.setSubject("Happy birthday to you!");
							 content = serverConfig.getBirthdayMessage().replace("@name", emp.getEmpFirstname().toUpperCase());
							 content += "\n\n" + serverConfig.getDefaultEmailSignature();
							 email.setContent(content);
							 email.setHTMLStyle(false);
							 listEmails.add(email);
						 }
					}
				} catch (Exception e) {
					CommonUtils.println(e.getMessage());
				}
			} 
		}
		if(listEmails.size() != 0){
			eMailSender.sendMail(listEmails);
		}
	}
	
	private Integer interval2Days(Date d1, Date d2) {
		try {
			if(d1 == null || d2 == null){
				return null;
			}
			Calendar cal1 = Calendar.getInstance();
			cal1.setTime(dateFormat.parse(dateFormat.format(d1)));
			
			Calendar cal2 = Calendar.getInstance();
			cal2.setTime(dateFormat.parse(dateFormat.format(d2)));
			
			if(cal1.after(cal2)){
				return null;
			}
			int daysBetween = 0;
			while (cal1.before(cal2)) {
				cal1.add(Calendar.DATE, 1);
				daysBetween ++;
				if(daysBetween > 2){
					return null;
				}
			}
			return daysBetween;
		} catch (Exception e) {
			return null;
		}
	}

	public ServerConfig getServerConfig() {
		return serverConfig;
	}

	public void setServerConfig(ServerConfig serverConfig) {
		this.serverConfig = serverConfig;
	}

	public BaseService getBaseService() {
		return baseService;
	}

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

	public EMailSender geteMailSender() {
		return eMailSender;
	}

	public void seteMailSender(EMailSender eMailSender) {
		this.eMailSender = eMailSender;
	} 
}
